﻿using System;
using System.Data;
using System.Data.SqlClient;
using ThangNM_LIB.EO;
using ThangNM_LIB.FUNC;
using ThangNM_LIB.DB;
using EO;
using System.Collections.Generic;

namespace DAO
{
    public class ColumnDAO : SQLiteDataProvider
    {
        public override Boolean bWriteLog { get { return false; } set { } }
        public override String sConnectionStrings { get { return ConstEx.connectionStrings.connSqLite_LOG; } set { } }

        public ColumnDAO(Boolean _bWriteLog = true, String _sConnectionStrings = ConstEx.connectionStrings.connSqLite_LOG)
        {
            this.bWriteLog = _bWriteLog;
            this.sConnectionStrings = _sConnectionStrings;
        }

        //public Boolean Log_CreateTable(String TableName)
        //{
        //    String sQuery = String.Format(ConstEx.QuerySqlite.Log_CreateTable, TableName);
        //    SqlCommand cmd = new SqlCommand(sQuery);
        //    cmd.CommandType = CommandType.Text;
        //    return ExecuteNonQuery(cmd);
        //}

        //public void Log_InsertItem(LogEO objLog)
        //{
        //    Log_CreateTable("tblLog");
        //    String sQuery = String.Format(ConstEx.QuerySqlite.Log_InsertItem, StringEx.Remove_Special_Character_SQLite(objLog.sContent), StringEx.Remove_Special_Character_SQLite(objLog.sUrl), StringEx.Remove_Special_Character_SQLite(objLog.sUrlRef), StringEx.Remove_Special_Character_SQLite(objLog.sIP), StringEx.Remove_Special_Character_SQLite(objLog.sDatabase), objLog.iType, objLog.dProcessTime, objLog.bStatus ? "1" : "0", objLog.tTimeBegin.ToString("yyyy-MM-dd HH:mm:ss.fff"), objLog.tTimeEnd.ToString("yyyy-MM-dd HH:mm:ss.fff"));
        //    sQuery = NullEx.getNullValueSQLite(sQuery);
        //    using (SqlCommand cmd = new SqlCommand(sQuery))
        //    {
        //        cmd.CommandType = CommandType.Text;
        //        DataTable dt = GetDataTable(cmd);
        //        //if (DataTableEx.DataTable_OK(dt))
        //        //{
        //        //    Int32 iCount = 0;
        //        //    Int32.TryParse(Convert.ToString(dt.Rows[0][0]), out iCount);
        //        //    if (iCount > Convert.ToInt32(OtherEx.getAppSettings("SIZE_LOG", "199")))
        //        //        SplitTable("tblLog");
        //        //}
        //    }
        //}

        //public DataTable Log_SelectBy_TableName(String sTableName)
        //{
        //    String sQuery = String.Format(ConstEx.QuerySqlite.Log_SelectBy_TableName, sTableName);
        //    SqlCommand cmd = new SqlCommand(sQuery);
        //    cmd.CommandType = CommandType.Text;
        //    return GetDataTable(cmd);
        //}

        public List<ColumnEO> SelectList()
        {
            List<ColumnEO> lstRet = new List<ColumnEO>();
            SqlCommand cmd = new SqlCommand("SELECT * FROM tblColumnSEO");
            cmd.CommandType = CommandType.Text;
            var result = (List<ColumnEO>)ConvertEx.ConvertDataTableToListObject<ColumnEO>(GetDataTable(cmd), lstRet.GetType());
            if (result != null && result.Count > 0)
            {
                lstRet = result;
            }
            return lstRet;
        }

        public Boolean InsertItem(ColumnEO objColumn)
        {
            String sQuery = "INSERT INTO tblColumnSEO (sColumnName, sDescription, sDataType, sDataTypeLength, bPrimaryKey, bForeignKey, bAllowNulls, bIdentity) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}');";
            sQuery = String.Format(sQuery, objColumn.sColumnName, objColumn.sDescription, objColumn.sSQL_DataType, objColumn.sSQL_DataLength, objColumn.bPrimaryKey ? "1" : "0", objColumn.bForeignKey ? "1" : "0", objColumn.bAllowNulls ? "1" : "0", objColumn.bIdentity ? "1" : "0");
            sQuery = NullEx.getNullValueSQLite(sQuery);
            SqlCommand cmd = new SqlCommand(sQuery);
            cmd.CommandType = CommandType.Text;
            return ExecuteNonQuery(cmd);
        }

        //public Boolean Log_DropTable(String sTableName)
        //{
        //    String sQuery = String.Format(ConstEx.QuerySqlite.Log_DropTable, sTableName);
        //    SqlCommand cmd = new SqlCommand(sQuery);
        //    cmd.CommandType = CommandType.Text;
        //    return ExecuteNonQuery(cmd);
        //}

        //public Boolean Log_DeleteDataTable(String sTableName)
        //{
        //    String sQuery = String.Format(ConstEx.QuerySqlite.Log_DeleteDataTable, sTableName);
        //    SqlCommand cmd = new SqlCommand(sQuery);
        //    cmd.CommandType = CommandType.Text;
        //    return ExecuteNonQuery(cmd);
        //}
    }
}
